package it.bela.pgcdc.common.utils;

import it.bela.pgcdc.common.bean.BaseBean;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class Utilities {
	
	private static final Logger log = Logger.getLogger(Utilities.class);
	
	public static BaseBean setDefaultParametersSelect(Object baseBeanObj) {
		
		BaseBean baseBean = (BaseBean) baseBeanObj;
		
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		baseBean.setOwnerUser(userDetails.getUsername());
		
		return baseBean;
	}
	
	public static BaseBean setDefaultParametersInsert(Object baseBeanObj) {
		
		BaseBean baseBean = (BaseBean) baseBeanObj;
		
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		baseBean.setOwnerUser(userDetails.getUsername());
		baseBean.setLastUpdate(new Date());
		
		return baseBean;
	}
	
	public static BaseBean setDefaultParametersUpdate(Object baseBeanObj) {
		
		BaseBean baseBean = (BaseBean) baseBeanObj;
		
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		baseBean.setOwnerUser(userDetails.getUsername());
		baseBean.setLastUpdate(new Date());
				
		return baseBean;
	}
	
	public static void deleteFiles(File dir) throws IOException{

		File[] files = dir.listFiles();

		if(files != null && files.length > 0)
			for (File file : files) {
				if (!file.isDirectory())
					file.delete();
			}
	}

	public static void copyFile(File inFile, File outFile) throws IOException{

		if(!inFile.isDirectory()){

			FileInputStream in = new FileInputStream(inFile);
			FileOutputStream out = new FileOutputStream(outFile);

			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0){
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
		}
	}
	
	public static boolean isValidFileName(String fileName){
		if (fileName != null && fileName != "" && !fileName.equals("empty") && !fileName.equals("delete")) 
			return true;
		else
			return false;
	}
	
	public static String padRight(String str, int size, char padChar){
		
		if (str == null || str.equals(""))
			return "";
		
		StringBuffer padded = new StringBuffer(str);
		while (padded.length() < size){
			padded.append(padChar);
		}
		return padded.toString();
	}
	
	public static String padLeft(String str, int size, char padChar){
		if (str == null || str.equals(""))
			return "";
		
		StringBuffer padding = new StringBuffer();
		while (padding.length() < size - str.length()){
			padding.append(padChar);
		}
		
		return padding.toString() + str;
	}
	
	public static java.sql.Date util2Sql(java.util.Date date) {
	    return new java.sql.Date(date.getTime());
	}
	
	public static java.util.Date sql2Util(java.sql.Date date) {
	    return new java.util.Date(date.getTime());
	}
	
	public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);
    
        // Get the size of the file
        long length = file.length();
    
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }
    
        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];
    
        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }
    
        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }
    
        // Close the input stream and return bytes
        is.close();
        return bytes;
    }
}
